﻿
Partial Class rss
    Inherits System.Web.UI.Page



    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim xml As New CTemplate(Server.MapPath("~/app_data/rss.xml"))

        Dim sb As New StringBuilder
        Dim item As New CTemplate(Server.MapPath("~/app_data/item.xml"))
        For Each i As CProduct In CProduct.Cache.Active.Sermons.MikeConnell.SortByCreatedDescName
            If i.Files.Count = 0 Then Continue For
            If Not i.Files.HasMp3 Then Continue For

            Dim name As String = i.ProductName
            Dim desc As String = i.ProductDescription
            Dim keyw As String = i.ProductKeywords
            If Not i.IsEnglish Then
                name = String.Concat(i.ProductNameChineseSimplified, " ", name)
                desc = String.Concat(i.ProductDescriptionChineseSimplified, vbCrLf, i.ProductDescription)
                keyw = String.Concat(i.ProductKeywordsChineseSimplified.Replace("，", ","), ",", keyw)
            End If

            item.Reset()
            item.Replace("ProductName", HttpUtility.HtmlEncode(name))
            item.Replace("UploadDate", i.ProductCreated.ToUniversalTime.ToString("ddd, dd MMM yyyy HH:mm:ss") & " GMT")
            item.Replace("Summary", HttpUtility.HtmlEncode(CUtilities.Truncate(desc, 2950)))
            If i.ProductKeywords.Length > 0 Then
                item.Replace("Keywords", HttpUtility.HtmlEncode(CUtilities.Truncate(keyw.Replace(vbCrLf, ","), 250)))
            Else
                item.Replace("Keywords", String.Empty)
            End If

            With i.Files.FirstMp3 'iTunes rejected the dropbox hosting, so have to route dropbox public via the web
                item.Replace("iTunesUrl", HttpUtility.HtmlEncode(CConfig.ItunesUrl(.FileName)))
                item.Replace("DropboxUrl", HttpUtility.HtmlEncode(CConfig.DropboxUrl(.FileName)))
                item.Replace("DownloadSize", .FileSize)
            End With

            sb.Append(item.Template)
        Next

        xml.Replace("SpeakerImage", HttpUtility.HtmlEncode(CSpeaker.Cache.GetById(ESpeaker.MikeConnell).SpeakerImage))
        xml.Replace("ItunesSummary", HttpUtility.HtmlEncode(CSetting.ItunesSummary))
        xml.Replace("ItunesDescription", HttpUtility.HtmlEncode(CSetting.ItunesDescription))
        xml.Replace("Items", sb.ToString)

        Response.ContentType = "application/rss+xml"
        Response.Write(xml.Template)
        Response.End()
    End Sub

End Class
